/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aplink.generic.autosync;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.NetworkErrorException;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;

import com.aplink.generic.pojo.MasterConfig;

@SuppressLint("Registered")
public class GenericAccountService extends Service {
	public class Authenticator extends AbstractAccountAuthenticator {
		public Authenticator(final Context context) {
			super(context);
		}

		@Override
		public Bundle addAccount(
				final AccountAuthenticatorResponse accountAuthenticatorResponse,
				final String s, final String s2, final String[] strings,
				final Bundle bundle) throws NetworkErrorException {
			return null;
		}

		@Override
		public Bundle confirmCredentials(
				final AccountAuthenticatorResponse accountAuthenticatorResponse,
				final Account account, final Bundle bundle)
						throws NetworkErrorException {
			return null;
		}

		@Override
		public Bundle editProperties(
				final AccountAuthenticatorResponse accountAuthenticatorResponse,
				final String s) {
			throw new UnsupportedOperationException();
		}

		@Override
		public Bundle getAuthToken(
				final AccountAuthenticatorResponse accountAuthenticatorResponse,
				final Account account, final String s, final Bundle bundle)
						throws NetworkErrorException {
			throw new UnsupportedOperationException();
		}

		@Override
		public String getAuthTokenLabel(final String s) {
			throw new UnsupportedOperationException();
		}

		@Override
		public Bundle hasFeatures(
				final AccountAuthenticatorResponse accountAuthenticatorResponse,
				final Account account, final String[] strings)
						throws NetworkErrorException {
			throw new UnsupportedOperationException();
		}

		@Override
		public Bundle updateCredentials(
				final AccountAuthenticatorResponse accountAuthenticatorResponse,
				final Account account, final String s, final Bundle bundle)
						throws NetworkErrorException {
			throw new UnsupportedOperationException();
		}
	}

	public static final String ACCOUNT_NAME = "sync";

	public static Account GetAccount(final MasterConfig masterConfig) {
		final String accountName = GenericAccountService.ACCOUNT_NAME;
		return new Account(accountName, masterConfig.getAutoSync()
				.getAuthorities());
	}

	private Authenticator mAuthenticator;

	@Override
	public IBinder onBind(final Intent intent) {
		return this.mAuthenticator.getIBinder();
	}

	@Override
	public void onCreate() {
		this.mAuthenticator = new Authenticator(this);
	}

	@Override
	public void onDestroy() {
	}

}
